package com.zhq.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;

import com.zhq.apputil.utils.LogUtil;
import com.zhq.apputil.widget.StateLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2016/4/1.
 * <p/>
 * 抽象Fragment模板
 */
public abstract class BaseFragment<V, P extends BasePresenter<V>> extends Fragment implements IView {
    private final String TAG = getClass().getSimpleName();

    public abstract int getLayoutUrl();

    public abstract void initData(Bundle savedInstanceState);

    public abstract StateLayout.Builder getBuilderLayout();

    protected abstract P createPresenter();

    public abstract void pullData();

    protected P iPresenter;

    private Unbinder unbinder;
    /***
     * 懒加载需要的布局
     */
    protected StateLayout rootView;
    /**
     * 是否初始化过布局
     */
    protected boolean isViewInitiated;
    /**
     * 当前界面是否可见
     */
    protected boolean isVisibleToUser;
    /**
     * 是否加载过数据
     */
    protected boolean isDataInitiated;

    public BaseFragment() {

    }

    @Override
    public void bindView() {
        iPresenter = createPresenter();
        if (iPresenter != null) {
            iPresenter.attach((V) this);
        }

    }

    @Override
    public void UnbindView() {
        if (iPresenter != null) {
            iPresenter.detachView();
        }
    }

    @Override
    public void onAttach(Context context) {
        LogUtil.e(TAG, "onAttach");
        super.onAttach(context);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtil.e(TAG, "onCreateView。。。。。");
        View view = inflater.inflate(getLayoutUrl(), container, false);
        unbinder = ButterKnife.bind(this, view);
        if (getBuilderLayout() != null) {
            rootView = getBuilderLayout().build();
            FrameLayout frameLayout = view.findViewById(0);//R.id.fragment_container
            frameLayout.addView(rootView);
        }
        bindView();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LogUtil.e(TAG, "onViewCreated。。。。。");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtil.e(TAG, "onActivityCreated。。。。。");
        isViewInitiated = true;
        initData(savedInstanceState);
    }

    /***
     * 通过activity onResume方法来判断 进行懒加载判断
     */
    @Override
    public void onResume() {
        super.onResume();
        LogUtil.e(TAG, "current state method is onResume isDataInitiated:" + isDataInitiated + " userVisible:" + getUserVisibleHint() + " isViewInitiated:" + isViewInitiated);
        if (isAdded() && getUserVisibleHint() && !isDataInitiated && isViewInitiated) {
            LogUtil.e(TAG, "onResume。。。。。isDataInitiated:数据加载.....");
            pullData();
            isDataInitiated = true;
        }
    }

    /***
     * 当前setUserVisibleHint方法，在viewPager+Fragment+FragmentPagerAdapter中会被调用
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtil.e(TAG, "current state method is setUserVisibleHint;" + isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (isVisibleToUser) {
            onVisible();
        } else {
            onInvisible();
        }
    }

    /**
     * fragment显示时才加载数据
     */
    protected void onVisible() {
        LogUtil.e(TAG, "current state method is onVisible;lazy loading.....");
        prepareFetchData(false);
    }


    /**
     * fragment隐藏
     */
    protected void onInvisible() {
    }

    /**
     * 隐藏recyclerView，显示加载布局
     *
     * @param recyclerView
     */
    public void showRootView(View recyclerView) {
        rootView.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
    }

    /**
     * 隐藏加载布局，显示recyclerView
     *
     * @param recyclerView
     **/
    public void hideRootView(View recyclerView) {
        rootView.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
    }

    /**
     * 判断懒加载条件
     *
     * @param forceUpdate
     */
    public void prepareFetchData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            pullData();
            isDataInitiated = true;
        }
    }

    /**
     * activity页面跳转
     *
     * @param activityClass
     */
    public void gotoActivity(Class<? extends Activity> activityClass) {
        Intent intent = new Intent(getActivity(), activityClass);
        startActivity(intent);
    }

    /**
     * activity页面带参数跳转
     *
     * @param activityClass
     * @param bundle
     */
    public void gotoActivity(Class<? extends Activity> activityClass, Bundle bundle) {
        Intent intent = new Intent(getActivity(), activityClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    /**
     * 关闭软键盘
     **/
    public void closeSoftKeyboard() {
        if (getActivity().getCurrentFocus() != null && getActivity().getCurrentFocus().getWindowToken() != null) {
            InputMethodManager manager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        UnbindView();
    }
}
